﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace ProjetoFinal
{

    public class Monkey
    {

        int frameTime; // TESTAR
        int frameNow; // 0
        int frames; // 5
        Vector2 size; //1197 X 196
        Vector2 frameSize;
        Vector2 framePosition; // 0 X 0

        Texture2D texture;
        public Rectangle rectDestination;
        public Rectangle rectSource;
        int frameRate;
        int moveRate;
        public Vector2 speed;
        public bool animation;
        public int animationTimer = 0;
        public int NFrame; // numero de frames
        public SoundEffect jumpSound; // colisão com a folha
        //SoundEffect collisionSound; *se combinar* colisão com as extremidades da tela
        SoundEffect failSound; // perder vida
        public bool teste = false;
        public bool backtoposition = false;
        bool help = true;
        public Monkey(Rectangle rectSource, Rectangle rectDestination, Vector2 speed, int frameRate, int moveRate, int frameTime, int frameNow, int frames, Vector2 frameSize, Vector2 size, Vector2 framePosition)
        {

            this.frameTime = frameTime;
            this.frameNow = frameNow;
            this.frames = frames;
            this.size = size;
            this.framePosition = framePosition;
            this.frameSize = frameSize;
            this.rectDestination = rectDestination;
            this.rectSource = rectSource;
            this.speed = speed;
            this.frameRate = frameRate;
            this.moveRate = moveRate;


        }

        public void LoadContent(ContentManager content)
        {
            texture = content.Load<Texture2D>("tileMonkey");
            jumpSound = content.Load<SoundEffect>("jumpSound");
            //collisionSound = content.Load<SoundEffect>("collisionSound");
            failSound = content.Load<SoundEffect>("failSound");
        }

        public void Move()
        {
            if (!(rectDestination.X >= 1024 - rectDestination.Width) || (rectDestination.X != 0))// se n passou da tela direita
            {

                rectDestination.X += (int)speed.X * -1;
                rectDestination.Y += (int)speed.Y * -1;
            }

            if ((rectDestination.X >= 1024 - rectDestination.Width) || (rectDestination.X == 0) || (rectDestination.Y > 768))
            {
                //collisionSound.Play();
                speed.X *= -1;

            }

            if (rectDestination.Y <= 0)
            {
                //collisionSound.Play();
                speed.Y *= -1;

            }


            Animation();

            if (rectDestination.Y + rectDestination.Height >= 768)// altura da tela
            {
                backtoposition = true; // chamar a função back to position la no mygame

                Console.WriteLine("back to position NO MONKEY: " + backtoposition);

            }

        }

        public void BackToPosition(ref int life, ref bool lostLife)
        {

            Console.WriteLine("back to position: " + backtoposition);

            // para a velocidade  sempre começar negativa pra subir
            if ((speed.X > 0) && (speed.Y > 0))
            {
                speed.X *= -1;
                // speed.Y *= -1;
            }

            // posicao inicial
            if (rectDestination.Y + rectDestination.Height >= 768)
            {
                failSound.Play();
                life--;
                lostLife = true;
                rectDestination.X = 400;
                rectDestination.Y = 577;

            }
        }

        /*public void Animation()
        {
            if (rectSource.X > texture.Width - moveRate)
            {
                animation = false;

            }
                
             
                if ((animationTimer == 20)) // tá na hora de trocar o quadro
                {
                    if ((rectDestination.Y >= 384)) // se passou da metade da tela
                    {
                            rectSource.X += moveRate;
                       
                    }

                    if ((rectDestination.Y < 384)) // se não chegou à metade da tela
                    {
                        if (teste) // marreta para mudar de frame só uma vez, e, com isso, chegar ao frame desejado, o segundo.
                        {
                            rectSource.X += moveRate;
                            teste = false;
                        }
                    }


                    animationTimer = 0;
                  

                }
            
        }*/
        public void Animation()
        {
            frameTime++;

            framePosition.Y = 0;

            if (frameTime > frameRate) // se está na hora de animar
            {
                if (frameNow < frames) // se não chegou no último frame
                {
                   
                    if ((rectDestination.Y <= 384)) // se passou da metade da tela
                    {
                        if (!help)
                        {
                            framePosition.X = 239 * frameNow; // mudou de quadro
                            frameNow++;
                            help = true;
                        }
                    }
                    if ((rectDestination.Y > 384)) // se não chegou à metade da tela
                    {
                        if (help)
                        {
                            framePosition.X = 239; // mudou de quadro
                            frameNow = 2;
                            help = false;
                        }
                       
                    }
                }
                else // se chegou ao último frame
                {
                    frameNow = 1;
                }
                frameTime = 0;
            }


        }


        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, rectDestination, 
                                      new Rectangle((int)framePosition.X, (int)framePosition.Y,(int)196, (int)192),
                                      Color.White);
        }

    }
}
